package cj.web.game.controllers;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.Page;
import cj.web.cc.controllers.BaseController;
import cj.web.cc.dao.OnlineOrderDAO;
import cj.web.constants.GameTypeEnum;
import cj.web.game.dao.GameDAO;

/**
 * 网络作文
 * @author Administrator
 *
 */
@Path("onlineFound")
public class OnlineFoundController extends BaseController{
	@Autowired
	private OnlineOrderDAO onlineOrderDAO;
	@Autowired
	private GameDAO gameDao;
	
	// ----------------------查询成绩
	/**
	 * WX查询成绩列表
	 * 
	 * @param model
	 * @return
	 */
	@Get("search")
	public String scoreSearch(Model model, HttpServletRequest request) {
		List<Map> reps = gameDao.queryAll(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("gamelis", reps);
		return "/views/onlineOrder/score/scoreSearch.vm";
	}
	
	/**
	 * WX查询成绩列表方法
	 * 
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@Post("dataExt")
	public String scoreSearchFun(@Param("code") String code,
			@Param("name") String name, @Param("gameId") int gameId,
			HttpServletRequest request) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);

		if ((!"".equals(code) && code.length() == 8)
				|| (!"".equals(name) && name.length() >= 2)) {
			int totalCount = onlineOrderDAO.queryReportCardByGCNSzie(gameId, code,
					name);
			if (totalCount == 0) {
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				jo.put("returnCode", Constants.returnObj.returnCode.code_2); // 没有数据
			}
		} else {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_1); // 输入的格式不对
		}

		return "@" + jo.toString();
	}

	/**
	 * WX查询成绩列表
	 * 
	 * @param model
	 * @return
	 */
	@Get("scoreList")
	public String scoreList(Model model, @Param("gameId") String gameId,
			@Param("name") String name, @Param("code") String code,
			HttpServletRequest request) {
		model.add("gameId", gameId);
		model.add("name", name);
		model.add("code", code);
		return "/views/onlineOrder/score/scoreList.vm";
	}
	
	/**
	 * WX查询成绩列表分页
	 * 
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@Post("lstData")
	public String scoreListFun(@Param("code") String code,
			@Param("name") String name, @Param("gameId") int gameId,
			@Param("start") int start, @Param("length") int length,
			HttpServletRequest request) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);

		if ((!"".equals(code) && code.length() == 8)
				|| (!"".equals(name) && name.length() >= 2)) {
			// 查询成绩列表
			List<Map> repsList = onlineOrderDAO.queryReportCardByGCN(gameId, code,
					name, start * length, length);

			for (Map rep : repsList) {
				int l_gameId = Integer.parseInt(rep.get("gameId").toString());
				String l_code = rep.get("code").toString();
				rep.put("state", l_gameId + URLPARAMS + l_code);
			}

			int totalCount = onlineOrderDAO.queryReportCardByGCNSzie(gameId, code,
					name);

			// 分页计算
			Page p = new Page<Map>(0, length, repsList, totalCount);
			jo.put("list", repsList);
			jo.put("repsize", p.getTotalPages());
			jo.put("totalCount", totalCount);
		} else {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}

		return "@" + jo.toString();
	}
}
